// #include "ComponentVitalStatsBase.h"
// #include "Terrain/Terrain.h"
// #include "Managers/BlocksManager.h"
// #include "Components/ComponentSickness.h"
// #include "Components/ComponentPlayer.h"
// #include "Components/ComponentBody.h"
// #include "Components/ComponentSleep.h"
// #include "Components/ComponentCreature.h"
// #include "Components/ComponentLevel.h"
// #include "Components/ComponentCreatureModel.h"
// #include "Components/ComponentHealth.h"
// #include "Components/ComponentClothing.h"
// #include "Blocks/WaterBlock.h"

// #include "Subsystems/SubsystemTimeOfDay.h"
// #include "Subsystems/SubsystemGameInfo.h"
// #include "Subsystems/SubsystemWeather.h"
// #include "Subsystems/BlockBehaviors/SubsystemMetersBlockBehavior.h"

// #include "Network/PackageManager.h"
// #include "Network/Packages/ComponentVitalStatPackage.h"
// #include "Game/PlayerData.h"
// #include "Log.h"
// #include "GameRandom.hpp"

// #include "GameSingleton.h"

// namespace PocketSurvival
// {
//     void ComponentVitalStatsBase::Update(float dt)
//     {
//         SubsystemTimeOfDay *timeOfDay = GameSingleton::singletonPtr->getSingleton<SubsystemTimeOfDay>();
//         SubsystemWeather *weather = GameSingleton::singletonPtr->getSingleton<SubsystemWeather>();
//         PackageManager *packageManager = GameSingleton::singletonPtr->getSingleton<PackageManager>();
//         SubsystemGameInfo *gameInfo = GameSingleton::singletonPtr->getSingleton<SubsystemGameInfo>();
//         SubsystemMetersBlockBehavior *m_metersBlockBehavior = GameSingleton::singletonPtr->getSingleton<SubsystemMetersBlockBehavior>();
//         GameRandom *random = GameSingleton::singletonPtr->getSingleton<GameRandom>();

//         auto view = GameSingleton::singletonPtr->getSingleton<GameManager>()->getEntityView<ComponentVitalStats *>();
//         for (auto entity : view)
//         {
//             ComponentVitalStats *vitalStats = view.get<ComponentVitalStats *>(entity);
//             ComponentPlayer *componentPlayer = vitalStats->componentPlayer;
//             ComponentBody *componentBody = componentPlayer->componentBody;
//             ComponentClothing *componentClothing = componentPlayer->componentClothing;

//             // updateFood
//             {
//                 Vector3 &velocity = componentBody->velocity;
//                 Vector2 velocityXZ(velocity.posX, velocity.posZ);
//                 float num2 = componentBody->componentCreature->componentCreatureModel->getEyePosition().posY - componentBody->position.posY;
//                 bool flag = componentBody->immersionDepth > num2;
//                 bool flag2 = componentBody->immersionFactor > 0.33f && componentPlayer->componentBody->standingOnValue == 0;
//                 bool flag3 = timeOfDay->periodicGameTimeEvent(240.0, 13.0) && false == componentPlayer->componentSickness->isSick();
//                 if (componentPlayer->playerData->gameMode != GameMode::Creative && gameInfo->areAdventureSurvivalMechanicsEnabled == true)
//                 {
//                     float hungerFactor = componentPlayer->componentLevel->getHungerFactor();
//                     float decFood = hungerFactor * dt / 2880.0f;
//                     if (flag2 || flag3)
//                     {
//                         decFood += hungerFactor * dt * velocityXZ.lengthSquared() / 1440.0f;
//                     }
//                     else
//                     {
//                         decFood += hungerFactor * dt * velocityXZ.lengthSquared() / 2880.0f;
//                     }
//                     if (velocity.posY > 0.0f)
//                     {
//                         decFood += hungerFactor * dt * velocity.posY / 900.0f;
//                     }
//                     vitalStats->setFood(vitalStats->food - decFood);

//                     if (componentPlayer->componentSleep->isSleeping() == false)
//                     {
//                         if (vitalStats->food <= 0.0f)
//                         {
//                             if (timeOfDay->periodicGameTimeEvent(30.0, 0.0))
//                             {
//                                 componentPlayer->componentHealth->injure(0.05f, entt::null, false, "饿死了");
//                             }
//                         }
//                     }
//                 }
//                 else
//                 {
//                     vitalStats->setFood(0.9f);
//                 }
//                 if (timeOfDay->periodicGameTimeEvent(1.0, -0.01))
//                 {
//                     vitalStats->satiationMapMutex.lock();
//                     for (auto it = vitalStats->satiationMap.begin(); it != vitalStats->satiationMap.end(); ++it)
//                     {
//                         it->second = MathUtils::Max<float>(it->second - 0.01 * dt, 0.0f);
//                     }
//                     vitalStats->satiationMapMutex.unlock();
//                 }
//             }
//             // updateStamina
//             {

//             }
//             // updateSleep
//             {
//                 bool isImmersion = componentBody->immersionFactor > 0.05f;
//                 if (componentPlayer->playerData->gameMode != GameMode::Creative && gameInfo->areAdventureSurvivalMechanicsEnabled == true)
//                 {
//                     if (componentPlayer->componentSleep->isSleeping())
//                     {
//                         vitalStats->setSleep(vitalStats->sleep + 0.05f * dt);
//                     }
//                     else if (isImmersion == false)
//                     {
//                         float amount = dt / 1800.0f;
//                         vitalStats->setSleep(vitalStats->sleep - amount);

//                         if (vitalStats->sleep <= 0.0f && componentPlayer->componentSleep->isSleeping())
//                         {
//                             componentPlayer->componentSleep->sleep(false);
//                         }
//                     }
//                 }
//                 else
//                 {
//                     vitalStats->setSleep(0.9f);
//                 }
//             }
//             // updateTemperature
//             {
//                 bool flag = timeOfDay->periodicGameTimeEvent(300.0, 17.0);
//                 float num = componentClothing->insulation * MathUtils::Lerp(1.0f, 0.05f, MathUtils::Saturate(4.0f * vitalStats->wetness));
//                 if (componentPlayer->playerData->gameMode <= GameMode::Survival)
//                 {
//                     num = num * 1.5f + 1.0f;
//                 }
//                 // 计算环境温度
//                 // ...
//                 if (timeOfDay->periodicGameTimeEvent(2.0, 0))
//                 {
//                     int32_t x = Terrain::ToCell(componentBody->position.posX);
//                     int32_t y = Terrain::ToCell(componentBody->position.posY + 0.1f);
//                     int32_t z = Terrain::ToCell(componentBody->position.posZ);
//                     m_metersBlockBehavior->calculateTemperature(x, y, z, 12.0f, num, vitalStats->environmentTemperature, vitalStats->environmentTemperatureFlux);

//                     // Log::info(fmt::format("temperature: {}, environmentTemperature: {}, environmentTemperatureFlux: {}", vitalStats->temperature, vitalStats->environmentTemperature, vitalStats->environmentTemperatureFlux));
//                 }
//                 if (componentPlayer->playerData->gameMode != GameMode::Creative && gameInfo->areAdventureSurvivalMechanicsEnabled == true)
//                 {
//                     float num2 = vitalStats->environmentTemperature - vitalStats->temperature;
//                     float num3 = 0.01f + 0.005f * vitalStats->environmentTemperatureFlux;
//                     vitalStats->setTemperature(vitalStats->temperature + MathUtils::Saturate(num3 * dt) * num2);
//                 }
//                 else
//                 {
//                     vitalStats->setTemperature(12.0f);
//                 }
//                 if (vitalStats->temperature <= 0.0f)
//                 {
//                     componentPlayer->componentHealth->injure(1.0f, entt::null, false, "被冻死了");
//                 }
//                 else if (vitalStats->temperature <= 3.0f)
//                 {
//                     if (timeOfDay->periodicGameTimeEvent(10.0, 0.0))
//                     {
//                         componentPlayer->componentHealth->injure(0.05f, entt::null, false, "被冻死了");
//                     }
//                 }
//                 if (vitalStats->temperature >= 24.0f)
//                 {
//                     if (timeOfDay->periodicGameTimeEvent(10.0, 0.0))
//                     {
//                         componentPlayer->componentHealth->injure(0.05f, entt::null, false, "被热死了");
//                     }
//                 }
//             }
//             // updateWetness
//             {
//                 if (componentBody->immersionFactor > 0.2f && componentBody->immersionFluidBlock->isBlock<WaterBlock *>() == true)
//                 {
//                     float num = 2.0f * componentPlayer->componentBody->immersionFactor;
//                     vitalStats->setWetness(vitalStats->wetness + MathUtils::Saturate(3.0f * dt) * (num - vitalStats->wetness));
//                 }
//                 int32_t x = Terrain::ToCell(componentPlayer->componentBody->position.posX);
//                 int32_t num2 = Terrain::ToCell(componentPlayer->componentBody->position.posY + 0.1f);
//                 int32_t z = Terrain::ToCell(componentPlayer->componentBody->position.posZ);

//                 PrecipitationShaftInfo precipitationShaftInfo = weather->getPrecipitationShaftInfo(x, z);
//                 if (num2 >= precipitationShaftInfo.YLimit && precipitationShaftInfo.Type == PrecipitationType::Rain)
//                 {
//                     vitalStats->setWetness(vitalStats->wetness + 0.05f * precipitationShaftInfo.Intensity * dt);
//                 }

//                 float num3 = 180.0f;
//                 if (vitalStats->environmentTemperature > 8.0f)
//                 {
//                     num3 = 120.0f;
//                 }
//                 if (vitalStats->environmentTemperature > 16.0f)
//                 {
//                     num3 = 60.0f;
//                 }
//                 if (vitalStats->environmentTemperature > 24.0f)
//                 {
//                     num3 = 30.0f;
//                 }
//                 vitalStats->setWetness(vitalStats->wetness - (dt / num3));
//             }

//             if (timeOfDay->periodicGameTimeEvent(1.0, 0.5))
//             {
//                 // spdlog::info("发送 vitalStats");
//                 std::shared_ptr<IPackage> package = std::make_shared<ComponentVitalStatPackage>(vitalStats);
//                 package->sendType = PackageSendType::TARGET;
//                 package->client = vitalStats->componentPlayer->playerData->client;
//                 packageManager->AddSendPackageToQueue(package);
//             }
//         }
//     }

//     void ComponentVitalStatsBase::applyDensityModifier(float modifier)
//     {
//         float num = modifier - densityModifierApplied;
//         if (num != 0.0f)
//         {
//             densityModifierApplied = modifier;
//             componentPlayer->componentBody->density += num;
//         }
//     }

//     static const std::string componentName = "VitalStats";
//     const std::string &ComponentVitalStatsBase::GetName()
//     {
//         return componentName;
//     }

// } // namespace PocketSurvival
